<!-- 系统登录页 -->
<template>
  <div class="page_login">
    <div class="page-center">
      <div class="info">
        <div class="title">Platform Login</div>
        <div class="detail">上海海事大学毕业设计</div>
      </div>

      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-position="left"
        label-width="0px"
        class="demo-ruleForm login-container"
      >
        <div class="title">
          <h3>欢迎用户登录</h3>
        </div>

        <!-- username -->
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            auto-complete="off"
            placeholder="用户名"
            prefix-icon="el-icon-s-custom"
          >
          </el-input>
        </el-form-item>

        <!-- password -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            auto-complete="off"
            placeholder="密码"
            prefix-icon="el-icon-edit"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <div
            style="display:flex;justify-content:space-between;align-items:center"
          >
            <!-- <el-checkbox v-model="isAutoLogin" checked class="remember">
              自动登录
            </el-checkbox> -->
            <router-link to="/register" tag="p">注册新用户</router-link>
          </div>
        </el-form-item>

        <!-- asubmit -->
        <el-form-item style="width:100%;">
          <el-button
            type="primary"
            style="width:100%;"
            @click="handleSubmit"
            :loading="logining"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  components: {},
  data() {
    return {
      logining: false,
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      isAutoLogin: true,
    };
  },
  props: {},
  computed: {},

  methods: {
    // 用户登录
    async handleSubmit() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          try {
            const res = await this.$http.post("/login", this.ruleForm);
            localStorage.clear();
            sessionStorage.username = res.data.user.username;
            sessionStorage.token = res.data.token;
            sessionStorage.userId = res.data.user._id;
            this.$router.go(-1);
            this.$message({
              message: `欢迎用户-${res.data.user.username}`,
              type: "success",
            });
          } catch (error) {
            console.log(error);
          }
        } else {
          // this.$message({
          //   type: "error",
          //   message: "Please check the email or password",
          // });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page_login {
  width: 100vw;
  height: 100vh;

  .page-center {
    position: absolute;
    width: 450px;
    height: 540px;
    margin: auto;
    top: 100px;
    right: 0;
    left: 0;

    .info {
      text-align: center;
      height: 100px;

      .title {
        display: flex;
        justify-content: center;
        align-items: Center;
        font-size: 32px;
        color: rgba(0, 0, 0, 0.85);
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
      }
      .detail {
        font-size: 18px;
        color: rgba(0, 0, 0, 0.45);
        margin-top: 30px;
      }
    }

    .login-container {
      -webkit-border-radius: 5px;
      border-radius: 5px;
      -moz-border-radius: 5px;
      background-clip: padding-box;
      padding: 10px 25px 15px 35px;
      background: #fff;
      border: 1px solid #eaeaea;
      box-shadow: 0 0 25px #cac6c6;
      .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
      }
    }
  }
}
</style>
